//
//  Patch.cpp
//  markerARLib
//
//  Created by fg on 16/5/8.
//  Copyright © 2016年 CvidKal. All rights reserved.
//

#include "Patch.hpp"




namespace markerAR
{
    

Patch::Patch(const unsigned char* center,const unsigned int stride,int PatchSize_)
{
    assert(PatchSize_%2 == 1);

    PatchSize = PatchSize_;
    halfPatch = PatchSize/2;
    value = new uchar*[PatchSize];
    gradient_ = new G_xy*[PatchSize];
    for (int i=0; i<PatchSize; ++i) {
        value[i] = new uchar[PatchSize];
        gradient_[i] = new G_xy[PatchSize];
    }
    
    patchImg = cv::Mat(PatchSize,PatchSize,CV_8U);
    const double epsilon = 0.;
    int sum = 0;
    for (int i= -halfPatch; i<=halfPatch; ++i)
    {
        for (int j= -halfPatch; j<=halfPatch; ++j)
        {
            int c = i*stride +j;
            int u = c - stride;
            int d = c +stride;
            int l = c - 1;
            int r = c +1;
            value[i+halfPatch][j+halfPatch] = (uchar)center[c];
            sum += center[c];
            patchImg.at<unsigned char>(j+halfPatch,i+halfPatch) = center[c];
            
            gradient_[i+halfPatch][j+halfPatch].gx = ((int)center[r]-center[l]+epsilon)/2.0;//dx
            gradient_[i+halfPatch][j+halfPatch].gy = ((int)center[d]-center[u]+epsilon)/2.0;//dy
            
        }
    }
    mean = sum/(PatchSize*PatchSize);
}

Patch::~Patch()
{
    for (int i=0; i<PatchSize; ++i) {
        delete[] value[i];
        delete[] gradient_[i];
    }
    delete [] value;
    delete [] gradient_;
}
}




